package javaer.starter.idea.window.component;

import org.apache.commons.collections.CollectionUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Map;
import java.util.Set;

/**
 * @author yangzhen
 * @date 2023/7/4 17:42
 * @since 1.0.0
 */
public class ModuleTable {

    public static final String[] fields = {"模块标识", "模块名称", "模块依赖", "是否启动模块", "是否必须"};

    private JTable moduleTable;

    public ModuleTable(JTable moduleTable) {
        this.moduleTable = moduleTable;
        DefaultTableModel model = new DefaultTableModel();
        for (String field : fields) {
            model.addColumn(field);
        }
        moduleTable.setModel(model);
    }

    private DefaultCellEditor getCellEditor() {
        DefaultComboBoxModel<Boolean> comboBoxModel = new DefaultComboBoxModel<>();
        comboBoxModel.addElement(true);  // 添加true选项
        comboBoxModel.addElement(false);  // 添加false选项
        JComboBox<Boolean> comboBox = new JComboBox<>(comboBoxModel);
        DefaultCellEditor cellEditor = new DefaultCellEditor(comboBox);
        return cellEditor;
    }

    public String getModuleName(String moduleId) {
        String value = getValue(0, moduleId, 1);
        return value;
    }

    public int isRequired(String moduleId) {
        return 1;
    }

    public int isStarter(String moduleId) {
        Boolean value = getValue(0, moduleId, 3);
        if (value == null) {
            return 0;
        }
        if (value) {
            return 1;
        }
        return 0;
    }

    public <T> T getValue(int checkColumn, String moduleId, int valueColumn) {
        DefaultTableModel model = (DefaultTableModel) moduleTable.getModel();
        int rowCount = model.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            String tableModuleId = (String) model.getValueAt(i, checkColumn);
            if (moduleId.equals(tableModuleId)) {
                return (T) model.getValueAt(i, valueColumn);
            }
        }
        return null;
    }

    /**
     * 初始化模块table数据
     */
    public void initModuleTableData(Set<String> artifactIds, Map<String, String> artifactId_description, Map<String, Set<String>> artifactId_moduleDependency) {
        DefaultTableModel model = (DefaultTableModel) moduleTable.getModel();
        model.setRowCount(0);
        moduleTable.getColumnModel().getColumn(3).setCellEditor(getCellEditor());
        moduleTable.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(new JTextField() {
            @Override
            public boolean isEditable() {
                return false;
            }
        }));
        int index = 0;
        for (String artifactId : artifactIds) {
            Object[] columnValue = new Object[5];
            columnValue[0] = artifactId;
            columnValue[1] = artifactId_description.get(artifactId);
            Set<String> moduleDependency = artifactId_moduleDependency.get(artifactId);
            if (CollectionUtils.isNotEmpty(moduleDependency)) {
                columnValue[2] = moduleDependency.stream()
                        .reduce((artifactId1, artifactId2) -> artifactId1 + ", " + artifactId2)
                        .orElse("");
            }
            DefaultCellEditor isStarter = (DefaultCellEditor) moduleTable.getCellEditor(index, 3);
            if (artifactIds.size() == 1 || artifactId.endsWith("-starter")) {
                ((JComboBox) isStarter.getComponent()).setSelectedItem(true);
                columnValue[3] = true;
            } else {
                ((JComboBox) isStarter.getComponent()).setSelectedItem(false);
                columnValue[3] = false;
            }
            columnValue[4] = true;
            model.addRow(columnValue);
            index++;
        }
    }
}
